package com.basswold.nasaesa.client;

import java.util.LinkedList;

/**
 * Provides access to available habitat modules.
 * @author Keith
 *
 */
public class ContructionList {

	/**
	 * Lists to hold available modules of each type.
	 */
	private LinkedList<ConstructionConnectorModule> plainModulesList;
	private LinkedList<ConstructionHabitatModule> dormModulesList;
	private LinkedList<ConstructionHabitatModule> sanitationModulesList;
	private LinkedList<ConstructionHabitatModule> foodWaterModulesList;
	private LinkedList<ConstructionHabitatModule> gymModulesList;
	private LinkedList<ConstructionHabitatModule> canteenModulesList;
	private LinkedList<ConstructionHabitatModule> powerModulesList;
	private LinkedList<ConstructionHabitatModule> controlModulesList;
	private LinkedList<ConstructionHabitatModule> airlockModulesList;
	private LinkedList<ConstructionHabitatModule> medicalModulesList;

	/**
	 * Constructor.
	 * @param modulesArray Integer array of available modules.
	 * listed by module ID.
	 */
	public ContructionList(final ModuleList modList) {
		
		plainModulesList = new LinkedList<ConstructionConnectorModule>();
		dormModulesList = new LinkedList<ConstructionHabitatModule>();
		sanitationModulesList = new LinkedList<ConstructionHabitatModule>();
		foodWaterModulesList = new LinkedList<ConstructionHabitatModule>();
		gymModulesList = new LinkedList<ConstructionHabitatModule>();
		canteenModulesList = new LinkedList<ConstructionHabitatModule>();
		powerModulesList = new LinkedList<ConstructionHabitatModule>();
		controlModulesList = new LinkedList<ConstructionHabitatModule>();
		airlockModulesList = new LinkedList<ConstructionHabitatModule>();
		medicalModulesList = new LinkedList<ConstructionHabitatModule>();
		
		while (modList.count() > 0) {
			Module module = modList.rm(0);
			String moduleType = module.getType();
			String condition = module.getCondition();
			String orientation = module.getOrientation();
			
//			if (condition != Module.condavail[4])	// Beyond repairable
			if (condition != Module.condavail[2])	// damaged
			{
				if (moduleType == Module.typeavail[0]) {	// Plain
					plainModulesList.add(new ConstructionConnectorModule(module.getId(), condition, orientation));
				} else if (moduleType == Module.typeavail[1]) {	// Dormitory
					dormModulesList.add(new ConstructionHabitatModule(module.getId(), condition, orientation));
				} else if (moduleType == Module.typeavail[2]) {	// Sanitation
					sanitationModulesList.add(new ConstructionHabitatModule(module.getId(), condition, orientation));
				} else if (moduleType == Module.typeavail[3]) {	// FoodWaterStorage
					foodWaterModulesList.add(new ConstructionHabitatModule(module.getId(), condition, orientation));
				} else if (moduleType == Module.typeavail[4]) { // Gym
					gymModulesList.add(new ConstructionHabitatModule(module.getId(), condition, orientation));
				} else if (moduleType == Module.typeavail[5]) {	// Canteen
					canteenModulesList.add(new ConstructionHabitatModule(module.getId(), condition, orientation));
				} else if (moduleType == Module.typeavail[6]) {	//Power
					powerModulesList.add(new ConstructionHabitatModule(module.getId(), condition, orientation));
				} else if (moduleType == Module.typeavail[7]) {	// Control
					controlModulesList.add(new ConstructionHabitatModule(module.getId(), condition, orientation));
				} else if (moduleType == Module.typeavail[8]) { // Airlock
					airlockModulesList.add(new ConstructionHabitatModule(module.getId(), condition, orientation));
				} else if (moduleType == Module.typeavail[9]) { // Medical
					medicalModulesList.add(new ConstructionHabitatModule(module.getId(), condition, orientation));
				} else {
					// Do nothing
				}
			}
		}

		/*
		for (int i = 0; i < modulesArray.length; i++) {

			int id = modulesArray[i];
			ConstructionModule.ModuleType type = ConstructionModule.getType(id);

			switch(type)
			{
			case PLAIN:
				plainModulesList.add(new ConstructionConnectorModule(id));
				break;
			case DORMITORY:
				dormModulesList.add(new ConstructionHabitatModule(id));
				break;
			case SANITATION:
				sanitationModulesList.add(new ConstructionHabitatModule(id));
				break;
			case FOOD_AND_WATER:
				foodWaterModulesList.add(new ConstructionHabitatModule(id));
				break;
			case GYM:
				gymModulesList.add(new ConstructionHabitatModule(id));
				break;
			case CANTEEN:
				canteenModulesList.add(new ConstructionHabitatModule(id));
				break;
			case POWER:
				powerModulesList.add(new ConstructionHabitatModule(id));
				break;
			case CONTROL:
				controlModulesList.add(new ConstructionHabitatModule(id));
				break;
			case AIRLOCK:
				airlockModulesList.add(new ConstructionHabitatModule(id));
				break;
			case MEDICAL:
				medicalModulesList.add(new ConstructionHabitatModule(id));
				break;
			default:
				// do nothing
				break;
			}
		}
		*/
	}
	
	/**
	 * Accessor for modules.
	 * @param type The type of module to get.
	 * @return A module of the type specified, or null if none available.
	 */
	public ConstructionModule getModuleOfType(ConstructionModule.ModuleType type) {
		
		switch(type)
		{
			case PLAIN:
				if (plainModulesList.isEmpty()) {
					return null;
				} else {
					return plainModulesList.removeFirst();
				}
			case DORMITORY:
				if (dormModulesList.isEmpty()) {
					return null;
				} else {
					return dormModulesList.removeFirst();
				}
			case SANITATION:
				if (sanitationModulesList.isEmpty()) {
					return null;
				} else {
					return sanitationModulesList.removeFirst();
				}
			case FOOD_AND_WATER:
				if (foodWaterModulesList.isEmpty()) {
					return null;
				} else {
					return foodWaterModulesList.removeFirst();
				}
			case GYM:
				if (gymModulesList.isEmpty()) {
					return null;
				} else {
					return gymModulesList.removeFirst();
				}
			case CANTEEN:
				if (canteenModulesList.isEmpty()) {
					return null;
				} else {
					return canteenModulesList.removeFirst();
				}
			case POWER:
				if (powerModulesList.isEmpty()) {
					return null;
				} else {
					return powerModulesList.removeFirst();
				}
			case CONTROL:
				if (controlModulesList.isEmpty()) {
					return null;
				} else {
					return controlModulesList.removeFirst();
				}
			case AIRLOCK:
				if (airlockModulesList.isEmpty()) {
					return null;
				} else {
					return airlockModulesList.removeFirst();
				}
			case MEDICAL:
				if (medicalModulesList.isEmpty()) {
					return null;
				} else {
					return medicalModulesList.removeFirst();
				}
			default:
				return null;
		}
	}
	
	/**
	 * Check if module list has any of a particular type of module.
	 * @param Type The type of module to check for.
	 * @return True if there any any modules in the list of the specified
	 * 		type. False otherwise.
	 */
	public boolean hasModuleOfType(ConstructionModule.ModuleType type) {
		
		switch(type)
		{
			case PLAIN:
				 return !plainModulesList.isEmpty();
			case DORMITORY:
				return !dormModulesList.isEmpty();
			case SANITATION:
				return !sanitationModulesList.isEmpty();
			case FOOD_AND_WATER:
				return !foodWaterModulesList.isEmpty();
			case GYM:
				return !gymModulesList.isEmpty();
			case CANTEEN:
				return !canteenModulesList.isEmpty();
			case POWER:
				return !powerModulesList.isEmpty();
			case CONTROL:
				return !controlModulesList.isEmpty();
			case AIRLOCK:
				return !airlockModulesList.isEmpty();
			case MEDICAL:
				return !medicalModulesList.isEmpty();
			default:
				return false;
		}
	}
	
	/**
	 * Add an existing module to the module list.
	 * @param type The type of module being added.
	 */
	public final void addModule(final ConstructionModule module) {
		
		if (module != null) {
			// Clear any connections this module has
			module.resetConnections();
			
			switch(module.getType())
			{
				case PLAIN:
					plainModulesList.add((ConstructionConnectorModule)module);
					break;
				case DORMITORY:
					dormModulesList.add((ConstructionHabitatModule)module);
					break;
				case SANITATION:
					sanitationModulesList.add((ConstructionHabitatModule)module);
					break;
				case FOOD_AND_WATER:
					foodWaterModulesList.add((ConstructionHabitatModule)module);
					break;
				case GYM:
					gymModulesList.add((ConstructionHabitatModule)module);
					break;
				case CANTEEN:
					canteenModulesList.add((ConstructionHabitatModule)module);
					break;
				case POWER:
					powerModulesList.add((ConstructionHabitatModule)module);
					break;
				case CONTROL:
					controlModulesList.add((ConstructionHabitatModule)module);
					break;
				case AIRLOCK:
					airlockModulesList.add((ConstructionHabitatModule)module);
					break;
				case MEDICAL:
					medicalModulesList.add((ConstructionHabitatModule)module);
					break;
				default:
					// do nothing
					break;
			}
		}		
	}
	
	/**
	 * Check how many modules of a specific type exists in the list.
	 * @param Type The type of module to check for.
	 * @return The number of that type of module currently in the list.
	 */
	public final int getNumModule(final ConstructionModule.ModuleType moduleType) {
		switch(moduleType)
		{
			case PLAIN:
				if (plainModulesList.isEmpty()) {
					return 0;
				}
				else {
					return plainModulesList.size();
				}
			case DORMITORY:
				if (dormModulesList.isEmpty()) {
					return 0;
				}
				else {
					return dormModulesList.size();
				}
			case SANITATION:
				if (sanitationModulesList.isEmpty()) {
					return 0;
				}
				else {
					return sanitationModulesList.size();
				}
			case FOOD_AND_WATER:
				if (foodWaterModulesList.isEmpty()) {
					return 0;
				}
				else {
					return foodWaterModulesList.size();
				}
			case GYM:
				if (gymModulesList.isEmpty()) {
					return 0;
				}
				else {
					return gymModulesList.size();
				}
			case CANTEEN:
				if (canteenModulesList.isEmpty()) {
					return 0;
				}
				else {
					return canteenModulesList.size();
				}
			case POWER:
				if (powerModulesList.isEmpty()) {
					return 0;
				}
				else {
					return powerModulesList.size();
				}
			case CONTROL:
				if (controlModulesList.isEmpty()) {
					return 0;
				}
				else {
					return controlModulesList.size();
				}
			case AIRLOCK:
				if (airlockModulesList.isEmpty()) {
					return 0;
				}
				else {
					return airlockModulesList.size();
				}
			case MEDICAL:
				if (medicalModulesList.isEmpty()) {
					return 0;
				}
				else {
					return medicalModulesList.size();
				}
			default:
				return 0;
		}		
	}	
}
